package com.ruoyi.jiaopei.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jiaopei.domain.bo.TbUserStudentBo;
import com.ruoyi.jiaopei.domain.vo.TbUserStudentVo;
import com.ruoyi.jiaopei.domain.TbUserStudent;
import com.ruoyi.jiaopei.mapper.TbUserStudentMapper;
import com.ruoyi.jiaopei.service.ITbUserStudentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户学生Service业务层处理
 *
 * @author macy
 * @date 2024-06-26
 */
@RequiredArgsConstructor
@Service
public class TbUserStudentServiceImpl implements ITbUserStudentService {

    private final TbUserStudentMapper baseMapper;

    /**
     * 查询用户学生
     */
    @Override
    public TbUserStudentVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户学生列表
     */
    @Override
    public TableDataInfo<TbUserStudentVo> queryPageList(TbUserStudentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbUserStudent> lqw = buildQueryWrapper(bo);
        Page<TbUserStudentVo> result = baseMapper.selectPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户学生列表
     */
    @Override
    public List<TbUserStudentVo> queryList(TbUserStudentBo bo) {
        LambdaQueryWrapper<TbUserStudent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbUserStudent> buildQueryWrapper(TbUserStudentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbUserStudent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId()!=null,TbUserStudent::getUserId,bo.getUserId());
        lqw.eq(bo.getStudentId()!=null,TbUserStudent::getStudentId,bo.getStudentId());
        return lqw;
    }

    /**
     * 新增用户学生
     */
    @Override
    public Boolean insertByBo(TbUserStudentBo bo) {
        TbUserStudent add = BeanUtil.toBean(bo, TbUserStudent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户学生
     */
    @Override
    public Boolean updateByBo(TbUserStudentBo bo) {
        TbUserStudent update = BeanUtil.toBean(bo, TbUserStudent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbUserStudent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户学生
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int update(Wrapper<TbUserStudent> updateWrapper) {
        return baseMapper.update(updateWrapper);
    }
}
